$(function () {
    var userInfoList


    $.ajax({
        url: "/getAllUserInfo",
        type: "get",
        success: function (data) {
            userInfoList = data
            changeSex(userInfoList)
            layui.use('table', function () {
                var table = layui.table
                var form = layui.form
                    , layer = layui.layer

/*                $.ajax({
                    url: '/getAllCompanyName',
                    type: 'get',
                    success: function (data) {
                        addOptions(data, "company")
                        // form.render('select','company')
                        form.render()
                    }
                })

                form.verify({
                    telephone: [
                        /^1[3-9]\d{9}$/
                        , '请输入正确的手机号'
                    ]
                })*/

                function tableRender(dataList) {
                    table.render({
                        elem: '#test'
                        , code: 0
                        , msg: ""
                        , data: dataList
                        , title: '用户数据表'
                        , cols: [[
                            {
                                field: 'id',
                                title: '序号',
                                width: '8%',
                                fixed: 'left',
                                unresize: true,
                                sort: true,
                            }

                            , {field: 'username', title: '用户名', width: '14%',}
                            , {field: 'password', title: '密码', width: '8%', sort: true}
                            , {field: 'sex', title: '性别', width: '15%', sort: true}
                            , {field: 'idcard', title: '身份证', width: '15%', sort: true}
                            , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: '10%', unresize: true}
                        ]],
                        page: true
                    });
                }

                form.on('select(condition)', function (data) {
                    $("#content").val("")
                    if (data.value == "all"){
                        $("#content").attr("placeholder","该条件无需输入查询内容");
                        $("#content").attr("disabled","disabled");
                        $("#search").attr("disabled","disabled");
                        $.ajax({
                            url: "/getAllUserInfo",
                            type: "get",
                            success: function (data) {
                                userInfoList = data
                                changeSex(userInfoList)
                                tableRender(userInfoList)
                            }
                        })

                    }else{
                        $("#content").removeAttr("disabled");
                        $("#content").attr("placeholder","请输入查询内容");
                        $("#search").removeAttr("disabled");
                        form.on('submit(search)', function (data) {
                            console.log(data.field.condition)
                            if (data.field.condition == "all"){

                            }else {
                                $.ajax({
                                    url: "/getNeedUserInfo",
                                    type: "post",
                                    data: {
                                        condition: data.field.condition,
                                        content: data.field.content
                                    },
                                    success: function (data) {
                                        userInfoList = data
                                        changeSex(userInfoList)
                                        tableRender(userInfoList)
                                    }
                                })
                            }

                            return false;
                        });
                    }
                });

                tableRender(userInfoList)



                //监听工具条
                table.on('tool(test)', function (obj) {
                    var data = obj.data;
                    userInfoId = data.id

                    if (obj.event === 'del') {
                        console.log(data)
                        layer.confirm('确定要删除吗', function (index) {
                            $.ajax({
                                url: "/deleteUserInfo",
                                type: "post",
                                data: {
                                    userInfoId: userInfoId
                                },
                                success: function () {

                                    obj.del();
                                    layer.close(index);
                                    userInfoList.splice($.inArray(data,userInfoList),1)
                                    tableRender(userInfoList)

                                }
                            })
                        });
                    } else if (obj.event === 'edit') {
                        layer.open({
                            id: '123',
                            type: 1,
                            area: ['400px', '450px'],
                            title: '编辑信息',
                            content: $("#changeuserinfo"),
                            shade: 0.5,
                            cancel: function (layero, index) {
                                layer.closeAll();
                            }
                        });

                        $.ajax({
                            url: '/getAllCompanyName',
                            type: 'get',
                            success: function (data) {
                                addOptions(data, "company")
                                // form.render('select','company')
                                form.render()
                            }
                        })

                        form.on('select(company)', function (data1) {
                            $.ajax({
                                url: '/getProject',
                                type: 'get',
                                data: {
                                    companyname: data1.value
                                },
                                success: function (res) {

                                    addOptions(res, "project")
                                    form.render()
                                }
                            })
                        });

                        $.ajax({
                            url: "/getOneUserInfo",
                            type: "get",
                            data: {
                                userInfoId: userInfoId,
                            },
                            dataType: 'json',
                            success: function (data) {
                                console.log(data.company)
                                $.ajax({
                                    url: '/getProject',
                                    type: 'get',
                                    data: {
                                        companyname: data.company
                                    },
                                    success: function (res) {
                                        addOptions2(res, "project", data.project)
                                        form.render()
                                    }
                                })

                                if (data.sex == 0) {
                                    data.sex = "男"
                                } else {
                                    data.sex = "女"
                                }
                                form.val('changeuserinfo', {
                                    "username": data.username,
                                    "sex": data.sex,
                                    "tel": data.tel,
                                    "company": data.company,
                                    "project": data.project,
                                });

                                form.on('submit(changeuserinfo)', function (data) {
                                    var sex
                                    if (data.field.sex == "男") {
                                        sex = 0
                                    } else {
                                        sex = 1
                                    }
                                    $.ajax({
                                        url: "/updateUserInfo",
                                        type: "post",
                                        data: {
                                            username: data.field.username,
                                            sex: sex,
                                            tel: data.field.tel,
                                            company: data.field.company,
                                            project: data.field.project,
                                            userInfoId: userInfoId
                                        },
                                        dataType: 'json',
                                        success: function (data) {
                                            layer.msg("修改成功");
                                            setTimeout(function () {
                                                window.location.reload()
                                            }, 1000);
                                        }
                                    })
                                    return false;
                                });
                            }
                        })


                    }
                });
            });
        }
    })


    $('body').click(function (e) {
        if (e.target.className == "layui-layer-shade") {
            layer.closeAll()
        }
    })

    function changeSex(resultList) {
        for (var i = 0; i < resultList.length; i++) {
            if (resultList[i].sex == 0) {
                resultList[i].sex = "男"
            } else {
                resultList[i].sex = "女"
            }
        }
    }

    function addOptions(data, str) {
        var s = "#" + str
        var pro = $(s);
        pro.empty()
        var options = '<option value="" selected=""></option>';
        for (var i = 0; i <= data.length - 1; i++) {
            options += '<option value="' + data[i] + '" >' + data[i] + '</option>';
        }
        pro.append(options);
    }

    function addOptions2(data, str, selectedItem) {
        var s = "#" + str
        var pro = $(s);
        pro.empty()
        var options = '<option value="" ></option>';
        for (var i = 0; i <= data.length - 1; i++) {
            if (data[i] == selectedItem) {
                options += '<option value="' + data[i] + '" selected="">' + data[i] + '</option>';
            } else {
                options += '<option value="' + data[i] + '" >' + data[i] + '</option>';
            }
        }
        pro.append(options);
    }
})
